package com.epay.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.system.entity.Parms;
import cn.stylefeng.guns.modular.system.entity.PlatMerOrder;
import com.alibaba.fastjson.JSONObject;
import com.epay.Epay;
import com.epay.EpayEncrypt;
import com.epay.EpayTransfer;
import com.epay.RespDataApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service("platMerOrderService")
@Slf4j
public class PlatMerOrderService {


    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 根据orderid
     *
     * @param orderId
     * @return
     */
    public PlatMerOrder getByOrderId(String orderId) throws Exception {
        JSONObject updateMyWechat = new JSONObject();
        updateMyWechat.put("orderId", orderId);



        String iv = RandomUtil.randomNumbers(16);
        String content = aesEncrypt(JSONObject.toJSONString(updateMyWechat),iv);
        log.error(" get order request api {}",JSONObject.toJSONString(updateMyWechat));
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.OPS_RECHARGE_API);
        String form = HttpUtil.createGet(parms.getVal() +"/platMerCodeApi/getByOrderId.api"+ "?reqParam=" + content + "&iv=" + iv).execute().body();
        log.error(" update my success rquest url" +
                " {} {}",parms.getVal() +"/platMerCodeApi/getByOrderId.api"+ "?reqParam=" + content + "&iv=" + iv,updateMyWechat.toJSONString());
        if(StringUtils.isEmpty(form)){
            throw new Exception( "order is exp");
        }
        return JSONObject.parseObject(form,PlatMerOrder.class);
    }

    public int success(PlatMerOrder byOrderId) {
        String iv = RandomUtil.randomNumbers(16);
        String content = aesEncrypt(JSONObject.toJSONString(byOrderId),iv);

        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.OPS_RECHARGE_API);
        String form = HttpUtil.createPost(parms.getVal() +"/platMerCodeApi/success.api"+ "?reqParam=" + content + "&iv=" + iv).execute().body();


        log.error(" update my success response {} {}",form,JSONObject.toJSONString(byOrderId));



        return 1;
    }
    private String aesEncrypt(String reqContent, String ivKey) {
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.AES_KEY_CODE);
        String aseKey = parms.getVal();
        return EpayEncrypt.phpAesEncrypt(aseKey, ivKey, reqContent);
    }
    public boolean updateMyWechat(String orderId,  BigDecimal realAmount) {

        JSONObject updateMyWechat = new JSONObject();
        updateMyWechat.put("orderId", orderId);
        updateMyWechat.put("stat", EpayTransfer.OrderStat.ARRIVED);
        updateMyWechat.put("sucessDate", new Date());
        updateMyWechat.put("taskStat", EpayTransfer.PushStat.NEW);



        if (realAmount == null) {


            updateMyWechat.put("realAmount", realAmount);
        } else {
            updateMyWechat.put("realAmount", realAmount);
        }
        String iv = RandomUtil.randomNumbers(16);
        String content = aesEncrypt(updateMyWechat.toJSONString(),iv);
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.OPS_RECHARGE_API);
        String form = HttpUtil.createPost(parms.getVal() +"/platMerCodeApi/updateMyWechat.api"+ "?reqParam=" + content + "&iv=" + iv).execute().body();
        log.error(" update my wechat response {} {}",form,updateMyWechat);

        RespDataApi respDataApi = JSONObject.parseObject(form ,RespDataApi.class);

        return Boolean.valueOf(respDataApi.getData().toString());

    }

    public RespDataApi<String> arrByAmountMerCode(String reqParam, String iv) {
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.OPS_RECHARGE_API);





        String form = HttpUtil.createPost(parms.getVal() +"/platMerCodeApi/arrByAmountMerCode.api"+ reqParam + "&iv=" + iv).execute().body();
        log.error(" update my wechat response {} {} {}",form,reqParam,iv);

        RespDataApi respDataApi = JSONObject.parseObject(form ,RespDataApi.class);

        return respDataApi;

    }
}
